/**
 * Candidate number: 35834
 */
package tv.starfu.game.states;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.audio.AudioRenderer;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.InputManager;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.nulldevice.NullSoundDevice;
import de.lessvoid.nifty.examples.controls.common.CommonBuilders;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import tv.starfu.sequencer.ActivationGrid;
import tv.starfu.sequencer.Trillian;
import tv.starfu.sequencer.model.SequencerModel;
import tv.starfu.sequencer.matrix.NiftySequencer;


public class GridState extends AbstractAppState implements AppState {
    
    private SimpleApplication app;
    private AppSettings settings;
    private AssetManager assetManager;
    private InputManager inputManager;

   // private ViewPort viewport;
   // private ViewPort guiViewport;
    private Node localRootNode = new Node("gridRootNode");
    private Node localGuiNode = new Node("gridGuiNode");
    
    private AudioRenderer audioRenderer;
    private Nifty nifty;
    //private Trillian trillian;
    private NiftyJmeDisplay niftyDisplay;
    private NiftySequencer niftySequencer;

    public SequencerModel sequencerModel;
    
    /**
     * New Grid (sequencer) state
     * @param app
     * @param settings 
     */
    public GridState(SimpleApplication app, AppSettings settings, SequencerModel sequencerModel) {
        this.app      = app;
        this.assetManager  = app.getAssetManager();
        this.inputManager  = app.getInputManager();
        this.settings      = settings; 
        this.sequencerModel = sequencerModel;
    
    }
    
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, (SimpleApplication)app); 
        
        /* Create Nifty-gui */
                    if (app instanceof SimpleApplication)
            {
               SimpleApplication simpleApp = (SimpleApplication) app;
 
             //  Nifty nifty;
               //NiftyJmeDisplay 
               niftyDisplay = new NiftyJmeDisplay(app.getAssetManager(), app.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort());
               nifty = niftyDisplay.getNifty();
               niftySequencer = new NiftySequencer(nifty, sequencerModel);
               
            }
                    
        
         
    }

    
    @Override
    public void update(float tpf) {
        
    }
    
    @Override
    public void cleanup() {
        super.cleanup();
        
        app.getGuiViewPort().removeProcessor(niftyDisplay);
        
        this.app.getGuiNode().detachChild(localGuiNode);
        this.app.getRootNode().detachChild(localRootNode);
    }
    
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if(enabled){         
            // Add Nifty-gui to viewport
            if(!app.getGuiViewPort().getProcessors().contains(niftyDisplay)) {
                    app.getGuiViewPort().addProcessor(niftyDisplay);
                    
            }
            
            niftySequencer.start(nifty);
            inputManager.setCursorVisible(true);
            
            this.app.getGuiNode().attachChild(localGuiNode);
            this.app.getRootNode().attachChild(localRootNode);
        } else {
             if(app.getGuiViewPort().getProcessors().contains(niftyDisplay)) { 
                    niftySequencer.clear(nifty);
            }    
            
                this.app.getGuiNode().detachChild(localGuiNode);
                this.app.getRootNode().detachChild(localRootNode);
        }
    }
    


}
